package com.web.app.modules.user.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.web.app.common.interceptor.LoginInterceptor;

public class UserAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	
	private String targetUrl;
	
	public String getTargetUrl() {
		return targetUrl;
	}
	
	public String login() {
		HttpServletRequest request = ServletActionContext.getRequest();
		
		if ("admin".equals(request.getParameter("username")) && "admin".equals(request.getParameter("password"))) {
			HttpSession session = ServletActionContext.getRequest().getSession();
			session.setAttribute(LoginInterceptor.USER, "admin");
			
			String targetUrl = (String) session.getAttribute(LoginInterceptor.TARGET_URL);
			if (StringUtils.isNoneEmpty(targetUrl)) {
				this.targetUrl = targetUrl;
			} else {
				this.targetUrl = "/index.jsp";
			}
			
			return SUCCESS;
		} else {
			request.setAttribute("error", "User name or password is invalid.");
		}
		
		return INPUT;
	}
	
	public String logout() {
		return SUCCESS;
	}
	
	public String gotoUser() {
		return SUCCESS;
	}

}
